function [capacity_cdma, interference_analysis, fig_cdma] = cdma_analysis(EbN0_dB, num_users, spreading_factors)
% CDMA多址接入技术分析
% 输入参数：
%   EbN0_dB - 信噪比范围 (dB)
%   num_users - 用户数量数组
%   spreading_factors - 扩频因子数组
% 输出参数：
%   capacity_cdma - CDMA容量
%   interference_analysis - 干扰分析结果
%   fig_cdma - 图形句柄

% 添加路径
addpath('../Common');
colors = color_definitions();

% 参数设置
chip_rate = 3.84e6; % 码片速率 (Hz)
symbol_rate = 15e3; % 符号速率 (Hz)
processing_gain_base = chip_rate / symbol_rate; % 基本处理增益
channel_gains = 1; % 信道增益
noise_power_dBm = -174 + 10*log10(symbol_rate); % 噪声功率
noise_power = 10^((noise_power_dBm-30)/10);

% 初始化结果数组
num_snr = length(EbN0_dB);
num_user_cases = length(num_users);
num_spreading_cases = length(spreading_factors);
capacity_cdma = zeros(num_snr, num_user_cases);
interference_analysis = zeros(num_user_cases, num_spreading_cases);

fprintf('CDMA多址接入技术分析...\n');

% CDMA容量和干扰分析
for snr_idx = 1:num_snr
    snr_linear = 10^(EbN0_dB(snr_idx)/10);
    
    for user_idx = 1:num_user_cases
        K = num_users(user_idx);
        
        % CDMA容量计算 (考虑多用户干扰)
        % 使用CDMA容量公式：C = W * log2(1 + S/(I + N))
        % 其中 I = (K-1)*S (假设完美功率控制)
        
        signal_power = snr_linear * noise_power;
        interference_power = (K-1) * signal_power; % 多用户干扰
        total_noise = noise_power + interference_power;
        
        effective_snr = signal_power / total_noise;
        
        % Shannon容量
        if effective_snr > 0
            capacity_cdma(snr_idx, user_idx) = chip_rate * log2(1 + effective_snr);
        else
            capacity_cdma(snr_idx, user_idx) = 0;
        end
    end
end

% 干扰分析 (固定SNR，变化用户数量和扩频因子)
snr_fixed_idx = round(num_snr/2);
for user_idx = 1:num_user_cases
    K = num_users(user_idx);
    
    for spreading_idx = 1:num_spreading_cases
        G = spreading_factors(spreading_idx); % 扩频因子
        
        % 计算SINR
        signal_power = 10^(EbN0_dB(snr_fixed_idx)/10) * noise_power;
        interference_power = (K-1) * signal_power / G; % 考虑扩频增益
        sinr = signal_power / (noise_power + interference_power);
        
        % 干扰分析结果
        interference_analysis(user_idx, spreading_idx) = sinr;
    end
end

%% 可视化结果
fig_cdma = figure('Name', 'CDMA多址接入技术分析', 'Position', [300, 100, 1200, 800]);

% 子图1: CDMA容量 vs SNR
subplot(2, 2, 1);
for user_idx = 1:num_user_cases
    plot(EbN0_dB, capacity_cdma(:, user_idx)/1e6, 'LineWidth', 2, 'Color', colors(user_idx, :));
    hold on;
end
grid on;
xlabel('Eb/N0 (dB)');
ylabel('容量 (Mbps)');
title('CDMA容量 vs 信噪比');
legend(arrayfun(@(x) sprintf('%d用户', x), num_users, 'UniformOutput', false), 'Location', 'NorthWest');

% 子图2: 干扰分析 (SINR vs 用户数量)
subplot(2, 2, 2);
for spreading_idx = 1:num_spreading_cases
    plot(num_users, 10*log10(interference_analysis(:, spreading_idx)), ...
         'o-', 'LineWidth', 2, 'Color', colors(spreading_idx, :));
    hold on;
end
grid on;
xlabel('用户数量');
ylabel('SINR (dB)');
title(sprintf('CDMA SINR vs 用户数量 (Eb/N0 = %d dB)', EbN0_dB(snr_fixed_idx)));
legend(arrayfun(@(x) sprintf('扩频因子%d', x), spreading_factors, 'UniformOutput', false), 'Location', 'SouthWest');

% 子图3: 容量 vs 扩频因子
subplot(2, 2, 3);
user_fixed_idx = min(3, num_user_cases); % 选择中等用户数量
bar(spreading_factors, capacity_cdma(snr_fixed_idx, user_fixed_idx)/1e6, 'FaceColor', colors(1, :));
xlabel('扩频因子');
ylabel('容量 (Mbps)');
title(sprintf('CDMA容量 vs 扩频因子 (%d用户, Eb/N0 = %d dB)', ...
              num_users(user_fixed_idx), EbN0_dB(snr_fixed_idx)));

% 子图4: 多用户检测性能比较
subplot(2, 2, 4);
% 比较传统检测和MMSE检测
mmse_gain = zeros(num_user_cases, 1);
for user_idx = 1:num_user_cases
    K = num_users(user_idx);
    % MMSE检测相对于传统检测的增益 (简化模型)
    mmse_gain(user_idx) = 10*log10(K) - 3; % 近似公式
end

plot(num_users, mmse_gain, 's-', 'LineWidth', 2, 'Color', colors(2, :));
grid on;
xlabel('用户数量');
ylabel('MMSE增益 (dB)');
title('CDMA多用户检测增益');

fprintf('CDMA分析完成！\n');

end